package cn.sh.tools.util;

import cn.hutool.core.util.StrUtil;
import cn.sh.tools.domain.other.Exif;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifSubIFDDescriptor;
import com.drew.metadata.exif.ExifSubIFDDirectory;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Description 照片 工具
 * @Classname PhotoComponent
 * @Date 2024/3/26
 * @Author sh
 **/
public class PhotoUtil {

    public static Exif getPhotoMetaInfo(String photoPath) {
        Metadata metadata;
        try {
            metadata = ImageMetadataReader.readMetadata(new File(photoPath));
            //printAllMetadata(metadata);
        } catch (ImageProcessingException | IOException e) {
            throw new RuntimeException("读取照片失败", e);
        }

        // 获取 EXIF数据
        Exif exif = new Exif();

        Exif.ExifIfd0 exifIfd0 = new Exif.ExifIfd0();
        exif.setExifIfd0(exifIfd0);
        ExifIFD0Directory exifIfd0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
        exifIfd0.setMake(exifIfd0Directory.getString(ExifIFD0Directory.TAG_MAKE));
        exifIfd0.setModel(exifIfd0Directory.getString(ExifIFD0Directory.TAG_MODEL));

        Exif.ExifSubIfd exifSubIfd = new Exif.ExifSubIfd();
        exif.setExifSubIfd(exifSubIfd);
        ExifSubIFDDirectory exifSubIfdDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
        ExifSubIFDDescriptor exifSubIfdDescriptor = new ExifSubIFDDescriptor(exifSubIfdDirectory);
        exifSubIfd.setFocalLength(exifSubIfdDescriptor.getFocalLengthDescription());
        exifSubIfd.setAperture(exifSubIfdDescriptor.getApertureValueDescription());
        exifSubIfd.setExposureTime(StrUtil.subBefore(exifSubIfdDescriptor.getExposureTimeDescription(), " ", true));
        exifSubIfd.setShutterSpeed(StrUtil.subBefore(exifSubIfdDescriptor.getShutterSpeedDescription(), " ", true));
        exifSubIfd.setIso("ISO " + exifSubIfdDescriptor.getIsoEquivalentDescription());
        exifSubIfd.setLensSpecification(exifSubIfdDescriptor.getLensSpecificationDescription());

        String dateTimeStr = exifSubIfdDirectory.getString(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss"));
        exifIfd0.setDateTime(localDateTime.format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss")));

        return exif;
    }

    private static void printAllMetadata(Metadata metadata) {
        // 遍历metadata中的所有directory
        for (Directory directory : metadata.getDirectories()) {
            System.out.println("------ " + directory.getName() + " ------");

            // 在directory中遍历所有tag
            for (Tag tag : directory.getTags()) {
                // 输出tag信息，其中tag含有tagName和描述（这是英文）
                System.out.println(tag.getTagName() + " = " + tag.getDescription());
            }

            if (directory.hasErrors()) {
                // 如果directory中有错误，输出错误信息
                for (String error : directory.getErrors()) {
                    System.err.println("ERROR: " + error);
                }
            }
        }
    }

}
